package com.oe.market.modules.base.model.dao;

import com.oe.market.modules.base.model.bean.Account;
import com.oe.market.modules.common.model.ADaoBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class AccountDao extends ADaoBase<Account> {
    private static final Logger log = LoggerFactory.getLogger(AccountDao.class);

    public static final AccountDao me = new AccountDao();

    @Override
    public Account getThisClassInstance() {
        return new Account();
    }

    @Override
    public String getTableName() {
        return "oeasy_account";
    }

    public Account fromSign(String user, String currentSign) throws SQLException {
        String sql = "select * from `" + getTableName() + "` where " + KeyValueEquation("user",(user))
                + "and" + KeyValueEquation("current_sign",(currentSign));
        List<Account> accounts = find(sql);
        if (0 == accounts.size()) {
            return null;
        }
        return accounts.get(0);
    }

    public Account checkUser(String user, String password) {
        String sql = "select * from `" + getTableName() + "` where " + KeyValueEquation("user",(user))
                + "and" + KeyValueEquation("password",(password));
        try {
            return find(sql).get(0);
        } catch (SQLException ignored) {
        }
        return null;
    }

    public String checkUser(String user) {
        try {
            String sql = "select * from `" + getTableName() + "` where " + KeyValueEquation("user",(user));
            List<Account> accountList = find(sql);
            if (0 != accountList.size()) {
                return "帐号已经存在.";
            }
        } catch (SQLException e) {
            return "非法请求.";
        }
        return null;
    }

    public String update(String user, Integer lastLogin, String cuurentSign) throws Exception {
        String sql = "UPDATE `" + getTableName() + "` SET `last_login`=" + lastLogin.toString()
                +", `current_sign`='" + cuurentSign + "' WHERE (`user`='"+ user + "');";

        int ret = executeSql(sql);
        if (0 == ret) {
            throw new Exception("更新账户信息失败");
        }
        return "更新成功";
    }

    public String updatePhone(Integer accountId, String phone) {
        String sql = "UPDATE `" + getTableName() + "` SET `bind_phone`='" + phone
                +"' WHERE (`account_id`="+ accountId + ");";
        return update(sql);
    }
    public boolean updateMail(String user, String mail) {
        String sql = "UPDATE `" + getTableName() + "` SET `bind_email`='" + mail
                +"' WHERE (`user`='"+ user + "');";

        try {
            int ret = executeSql(sql);
            if (0 != ret) {
                return true;
            }
        }catch (SQLException ignored) {
        }
        return false;
    }
//
//    @Override
//    protected Account query(Map map) {
//        Account account = new Account();
//        for (Object key : map.keySet()) {
//            try {
//                account.setProperty((String) key, map.get(key));
//            } catch (Exception e) {
//                System.out.println(e.getMessage());
//            }
//        }
//        return account;
//    }
}